import data.Admin;
import data.User;
import view.AdminUi;
import data.Product;
import view.ShoppingCart;
import view.UserUi;

import java.util.ArrayList;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // 创建数据列表
        ArrayList<User> users = new ArrayList<>();
        ArrayList<Admin> admins = new ArrayList<>();
        ArrayList<Product> products = new ArrayList<>();
        ArrayList<Product> cart = new ArrayList<>();
        ArrayList<Product> purchaseHistory = new ArrayList<>();

        // 初始化数据
        Product product1 = new Product("00001", "小米14", "小米", "20231001", "mi14", 2999, 3999, 10);
        Product product2 = new Product("00002", "小米手环9", "小米", "20240701", "miband9", 199, 299, 100);
        Product product3 = new Product("00003", "华为平板2024", "华为", "20240806", "huaweipad2024", 4999, 5999, 5);
        Product product4 = new Product("00004", "华为手表5", "华为", "20240601", "huaweiwatch5", 1999, 2499, 20);
        Admin admin = new Admin("admin", "ynuinfo#777");
        User user1 = new User("1", "张三123", "金牌用户", 20240810, 1250, "123456", "123456@qq.com");
        User user2 = new User("2", "李四123", "银牌用户", 20230809, 99, "1234567", "1234567@qq.com");
        User user3 = new User("3", "王五123", "铜牌用户", 20220810, 900, "12345678", "12345678@qq.com");

        // 将数据添加到列表
        users.add(user1);
        users.add(user2);
        users.add(user3);
        admins.add(admin);
        products.add(product1);
        products.add(product2);
        products.add(product3);
        products.add(product4);

        // 主程序循环
        while (true) {
            // 打印主菜单
            System.out.println("——————————————————————————————————————————————");
            System.out.println("*               购物管理系统                   *");
            System.out.println("*请选择操作：1管理员系统                         *");
            System.out.println("*          2用户购物系统                       *");
            System.out.println("*          3退出                             *");
            System.out.println("——————————————————————————————————————————————");

            Scanner scanner = new Scanner(System.in);
            String choice = scanner.next();

            switch (choice) {
                case "1": // 管理员系统
                    String adminPassword = AdminUi.login(admins);
                    if (!adminPassword.equals("0")) {
                        boolean isAdminLoggedIn = true;
                        while (isAdminLoggedIn) {
                            System.out.println("请选择您要使用的功能：1-密码管理，2-客户管理，3-商品管理，4-退出登录");
                            String adminChoice = scanner.next();

                            switch (adminChoice) {
                                case "1": // 密码管理
                                    System.out.println("请选择你要修改的密码：1-自身密码 2-用户密码");
                                    String passwordChoice = scanner.next();
                                    switch (passwordChoice) {
                                        case "1":
                                            AdminUi.modifyAdminPassword(admins);
                                            break;
                                        case "2":
                                            AdminUi.resetUserPassword(users);
                                            break;
                                        default:
                                            System.out.println("输入不合法");
                                    }
                                    break;
                                case "2": // 客户管理
                                    System.out.println("请选择：1-列出客户信息 2-删除客户信息 3-查询客户信息 ");
                                    String customerChoice = scanner.next();
                                    switch (customerChoice) {
                                        case "1":
                                            AdminUi.printUserList(users);
                                            break;
                                        case "2":
                                            AdminUi.removeUser(users);
                                            break;
                                        case "3":
                                            AdminUi.queryUser(users);
                                            break;
                                        default:
                                            System.out.println("没有这个选项");
                                    }
                                    break;
                                case "3": // 商品管理
                                    System.out.println("请选择：1-列出所有商品 2-添加商品 3-修改商品 4-删除商品 5-查询商品 ");
                                    String productChoice = scanner.next();
                                    switch (productChoice) {
                                        case "1":
                                            AdminUi.printProductList(products);
                                            break;
                                        case "2":
                                            AdminUi.addProduct(products);
                                            break;
                                        case "3":
                                            AdminUi.modifyProduct(products);
                                            break;
                                        case "4":
                                            AdminUi.removeProduct(products);
                                            break;
                                        case "5":
                                            System.out.println("请输入要查询的商品名称、生产厂家或零售价格:");
                                            String searchCriteria = scanner.next();
                                            AdminUi.searchProduct(products, searchCriteria);
                                            break;
                                        default:
                                            System.out.println("没有这个选项");
                                    }
                                    break;
                                case "4":
                                    System.out.println("已退出管理员系统");
                                    isAdminLoggedIn = false;
                                    break;
                                default:
                                    System.out.println("没有这个选项");
                            }
                        }
                    }
                    break;

                case "2": // 用户系统
                    boolean isUserLoggedIn = true;
                    while (isUserLoggedIn) {
                        System.out.println("欢迎使用用户系统！");
                        System.out.println("请选择您要使用的功能：1-用户注册 2-用户登录 3-修改密码 4-忘记密码 5-购物 6-退出登录");
                        String userChoice = scanner.next();

                        switch (userChoice) {
                            case "1":
                                UserUi.register(users);
                                break;
                            case "2":
                                UserUi.login(users);
                                break;
                            case "3":
                                UserUi.changePassword(users);
                                break;
                            case "4":
                                UserUi.forgetPassword(users);
                                break;
                            case "5": // 购物功能
                                boolean isShopping = true;
                                while (isShopping) {
                                    System.out.println("请选择：1-商品加入购物车 2-商品移除购物车 3-修改购物车中商品 4-结账 5-查看购物历史 6-退出购物系统 ");
                                    String shoppingChoice = scanner.next();
                                    switch (shoppingChoice) {
                                        case "1":
                                            System.out.println("请输入商品编号和数量：");
                                            String productId = scanner.next();
                                            int quantity = scanner.nextInt();
                                            ShoppingCart.addProduct(productId, quantity, products, cart);
                                            ShoppingCart.viewCart(cart);
                                            break;
                                        case "2":
                                            System.out.println("请输入要移除的商品编号：");
                                            String removeProductId = scanner.next();
                                            ShoppingCart.removeProduct(removeProductId, cart);
                                            ShoppingCart.viewCart(cart);
                                            break;
                                        case "3":
                                            System.out.println("请输入要修改的商品编号：");
                                            String modifyProductId = scanner.next();
                                            ShoppingCart.modifyProduct(modifyProductId, cart);
                                            break;
                                        case "4":
                                            System.out.println("请选择付款方式（支付宝，微信，银行卡）:");
                                            String paymentMethod = scanner.next();
                                            ShoppingCart.checkout(paymentMethod, products, cart, purchaseHistory);
                                            break;
                                        case "5":
                                            ShoppingCart.viewPurchaseHistory(purchaseHistory);
                                            break;
                                        case "6":
                                            System.out.println("已退出购物系统");
                                            isShopping = false;
                                            break;
                                        default:
                                            System.out.println("没有这个选项");
                                    }
                                }
                                break;
                            case "6":
                                System.out.println("已退出用户系统");
                                isUserLoggedIn = false;
                                break;
                            default:
                                System.out.println("没有这个选项");
                        }
                    }
                    break;

                case "3":
                    System.out.println("感谢使用！");
                    System.exit(0);
                    break;

                default:
                    System.out.println("没有这个选项");
            }
        }
    }
}
